#include "main.h"
#include "parameter.h"
#include "stdio.h"

//Information block 不能写入
//	0x1FFF D800 - 0x1FFF F7FF 8 Kbytes
//0x0803 F800 - 0x0803 FFFF 2 Kbytes Page 127
//存储内容：设备号、设备信道、空调控制方式-IRcode-自定义、工作模式、温控参数、时控参数。
//该地址不能读写。
//#define DEVICE_PARAMETER_ADDR  0x1FFFD800
//定义最后1个page用于存储数据
#define DEVICE_CONTROL_PARA_ADDR  0x0803F800

#define DEVICE_TIME_PARA_ADDR  0x0803FA00

uint32_t WriteFlashData = 0x12345678;
//uint32_t addr = 0x0807E000;
//uint32_t addr = 0x0803E000; //256K
uint32_t addr = DEVICE_CONTROL_PARA_ADDR;


/*FLASH写入程序*/
void writeFlashTest(void)
{
	/* 1/4解锁FLASH*/
	HAL_FLASH_Unlock();

	/* 2/4擦除FLASH*/
	/*初始化FLASH_EraseInitTypeDef*/
	/*擦除方式页擦除FLASH_TYPEERASE_PAGES，块擦除FLASH_TYPEERASE_MASSERASE*/
	/*擦除页数*/
	/*擦除地址*/
	FLASH_EraseInitTypeDef FlashSet;
	FlashSet.TypeErase = FLASH_TYPEERASE_PAGES;
	FlashSet.PageAddress = addr;
	FlashSet.NbPages = 1;

	/*设置PageError，调用擦除函数*/
	uint32_t PageError = 0;
	HAL_FLASHEx_Erase(&FlashSet, &PageError);

	/* 3/4对FLASH烧写*/
//	str_Device_Param
	HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, addr, WriteFlashData);
//	HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, addr, WriteFlashData);
	/* 4/4锁住FLASH*/
	HAL_FLASH_Lock();
}

/*FLASH读取打印程序*/
void printFlashTest(void)
{
	uint32_t temp = *(__IO uint32_t*)(addr);

	printf("addr is:0x%lx, data is:0x%lx\r\n", addr, temp);
}

void EraseDataFromFlash(void)
{
	addr = DEVICE_CONTROL_PARA_ADDR;
	/* 1/4解锁FLASH*/
		HAL_FLASH_Unlock();

		/* 2/4擦除FLASH*/
		/*初始化FLASH_EraseInitTypeDef*/
		/*擦除方式页擦除FLASH_TYPEERASE_PAGES，块擦除FLASH_TYPEERASE_MASSERASE*/
		/*擦除页数*/
		/*擦除地址*/
		FLASH_EraseInitTypeDef FlashSet;
		FlashSet.TypeErase = FLASH_TYPEERASE_PAGES;
		FlashSet.PageAddress = addr;
		FlashSet.NbPages = 1;

		/*设置PageError，调用擦除函数*/
		uint32_t PageError = 0;
		HAL_FLASHEx_Erase(&FlashSet, &PageError);
	/* 4/4锁住FLASH*/
			HAL_FLASH_Lock();
}

void writeFlash(void)
{
	int i;
	/* 1/4解锁FLASH*/
		HAL_FLASH_Unlock();

		/* 2/4擦除FLASH*/
		/*初始化FLASH_EraseInitTypeDef*/
		/*擦除方式页擦除FLASH_TYPEERASE_PAGES，块擦除FLASH_TYPEERASE_MASSERASE*/
		/*擦除页数*/
		/*擦除地址*/
		FLASH_EraseInitTypeDef FlashSet;
		FlashSet.TypeErase = FLASH_TYPEERASE_PAGES;
		FlashSet.PageAddress = addr;
		FlashSet.NbPages = 1;

		/*设置PageError，调用擦除函数*/
		uint32_t PageError = 0;
		HAL_FLASHEx_Erase(&FlashSet, &PageError);

		/* 3/4对FLASH烧写*/

//		HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, addr, uc_DevParm[i]);

		addr = DEVICE_CONTROL_PARA_ADDR;
		for(i=0;i<10;i++,addr+=2)
		{
//			HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, addr, str_Device_Param.uc_DevParm[i]);
			HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, addr, str_Device_Param.uc_DevParm[i]);
		}

		addr = DEVICE_TIME_PARA_ADDR;
		HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, addr, TimeCtrlCount);
		addr+=2;
		for(i=0;i<18;i++)
		{
			HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, addr, str_TimeCtrl_Param.uc_DevTimeParm[0][i]);
			addr+=2;
			HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, addr, str_TimeCtrl_Param.uc_DevTimeParm[1][i]);
			addr+=2;
			HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, addr, str_TimeCtrl_Param.uc_DevTimeParm[2][i]);
			addr+=2;
			HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, addr, str_TimeCtrl_Param.uc_DevTimeParm[3][i]);
			addr+=2;
			__ASM("NOP");
		}
		/* 4/4锁住FLASH*/
		HAL_FLASH_Lock();
}

void ReadDevPara(void)
{
	int i;
	addr = DEVICE_CONTROL_PARA_ADDR;
	for(i=0;i<10;i++,addr+=2)
	{
		str_Device_Param.uc_DevParm[i] = *(__IO uint16_t*)(addr);
		__ASM("NOP");
	}
	addr = DEVICE_TIME_PARA_ADDR;
	TimeCtrlCount = *(__IO uint16_t*)(addr);
	addr+=2;
	for(i=0;i<18;i++)
	{
		str_TimeCtrl_Param.uc_DevTimeParm[0][i] = *(__IO uint16_t*)(addr);
		addr+=2;
		str_TimeCtrl_Param.uc_DevTimeParm[1][i] = *(__IO uint16_t*)(addr);
		addr+=2;
		str_TimeCtrl_Param.uc_DevTimeParm[2][i] = *(__IO uint16_t*)(addr);
		addr+=2;
		str_TimeCtrl_Param.uc_DevTimeParm[3][i] = *(__IO uint16_t*)(addr);
		addr+=2;
		__ASM("NOP");
	}
}

void ReadFlash(void)
{
	int i;
	addr = DEVICE_CONTROL_PARA_ADDR;
	for(i=0;i<10;i++,addr+=2)
	{
		str_Device_Param.uc_DevParm[i] = *(__IO uint16_t*)(addr);
		__ASM("NOP");
	}

}


void InitDevPara(void)
{
	int i;
	addr = DEVICE_CONTROL_PARA_ADDR;
	for(i=0;i<10;i++,addr+=2)
	{
		str_Device_Param.uc_DevParm[i] = *(__IO uint16_t*)(addr);
		__ASM("NOP");
	}
}

void ReadTimeTable(void)
{
	int i;
	addr = DEVICE_TIME_PARA_ADDR;
		for(i=0;i<18;i++)
		{
			str_TimeCtrl_Param.uc_DevTimeParm[0][i] = *(__IO uint16_t*)(addr);
			addr+=2;
			str_TimeCtrl_Param.uc_DevTimeParm[1][i] = *(__IO uint16_t*)(addr);
			addr+=2;
			str_TimeCtrl_Param.uc_DevTimeParm[2][i] = *(__IO uint16_t*)(addr);
			addr+=2;
			str_TimeCtrl_Param.uc_DevTimeParm[3][i] = *(__IO uint16_t*)(addr);
			addr+=2;
			__ASM("NOP");
		}

}

void Flash_Test(void)
{
	/*Flash TEST*/
//	   WriteFlashData = 0x12345678;
//	   writeFlashTest();
//	   printFlashTest();
//
//	   addr += 4;
//	   WriteFlashData = 0x87654321;
//	   writeFlashTest();
//	   printFlashTest();
//
//	   addr += 4;
//	   WriteFlashData = 0x13572468;
//	   writeFlashTest();
//	   printFlashTest();

	str_Device_Param.str_DeviceParam.DevNum  = 0x0102;
	str_Device_Param.str_DeviceParam.Channel = 0x12;
	str_Device_Param.str_DeviceParam.AirContronlMode = 1;
	str_Device_Param.str_DeviceParam.WorkMode = 1;
	str_Device_Param.str_DeviceParam.Temperature_L = 26;
	str_Device_Param.str_DeviceParam.Temperature_H = 32;
	str_Device_Param.str_DeviceParam.IRUseDefCodeFlag = 0;

	writeFlash();
//

	str_Device_Param.str_DeviceParam.Temperature_L = 21;
	str_Device_Param.str_DeviceParam.Temperature_H = 30;
	str_Device_Param.str_DeviceParam.IRUseDefCodeFlag = 0;

	ReadFlash();


	printf("DevNum:%x\r\n",str_Device_Param.str_DeviceParam.DevNum);
	printf("Channel:%x\r\n",str_Device_Param.str_DeviceParam.Channel);
	printf("AirContronlMode:%x\r\n",str_Device_Param.str_DeviceParam.AirContronlMode);
	printf("WorkMode:%x\r\n",str_Device_Param.str_DeviceParam.WorkMode);
	printf("Temperature_L:%d\r\n",str_Device_Param.str_DeviceParam.Temperature_L);
	printf("Temperature_H:%d\r\n",str_Device_Param.str_DeviceParam.Temperature_H);
	printf("IRUseDefCodeFlag:%x\r\n",str_Device_Param.str_DeviceParam.IRUseDefCodeFlag);

}
















